package com.df.barcodescaning;

import android.Manifest;
import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.df.barcodescaning.bean.UserData;
import com.df.barcodescaning.bean.UserGroup;
import com.df.barcodescaning.util.CSVUtils;
import com.df.barcodescaning.util.FileLocalCache;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.df.barcodescaning.util.FileLocalCache.delSerializableData;
import static com.df.barcodescaning.util.FileLocalCache.getSerializableData;
import static com.df.barcodescaning.util.FileLocalCache.setSerializableData;

/**
 * Created by G on 2018/5/23.
 */

public class MyApplication extends Application {

    public static final String LOGIN_USER = "the login user info";
    public static final String REGISTED_USER = "registed user";

    private static MyApplication mInstance;

//    public MixSpeakUtil mixSpeakUtil;
    //创建震动服务对象
//    public Vibrator mVibrator;

    private List<String> dataList;
    private UserData loggedUserData;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static MyApplication getInstance() {
        return mInstance;
    }

    public List<String> getDataList() {
        if (dataList!= null && dataList.size() > 0) return dataList;
        ///storage/sdcard0/Download/test.csv
        String path = "/storage/sdcard0/Download/liaohao.csv";
        return dataList = CSVUtils.importCsv(new File(path));
    }

    /**
     * 获取本机注册的用户
     * @return
     */
    public UserGroup getRegistedUser() {
        UserGroup userGroup = (UserGroup) FileLocalCache.getSerializableData(mInstance, REGISTED_USER);
        if (userGroup == null) {
            userGroup = new UserGroup();
            Map<String, UserData> map = new HashMap<>();
            map.put("Admin", new UserData().getManager());
            userGroup.setValues(map);
            saveRegistedUser(userGroup);
        }
        return userGroup;
    }

    /**
     * 保存所有用户信息
     */
    public void saveRegistedUser(UserGroup group){
        setSerializableData(this, group, REGISTED_USER);
    }

    /**
     * 保存用户登录信息
     */
    public void saveLoggedUser(UserData member){
        setSerializableData(this, member, LOGIN_USER);
    }

    /**
     * 获取已登录用户
     * @return
     */
    public UserData getLoggedUser() {
        return (UserData) getSerializableData(this, LOGIN_USER);
    }

//    public String getLoggedUserName() {
//        return loggedUserData.getUserName();
//    }
//
//    public void setLoggedUserName(UserData userData) {
//        loggedUserData = userData;
//    }

    /**
     * 退出登录, 删除用户登录信息
     */
    public void deleteLoggedUser() {
        delSerializableData(this, LOGIN_USER);
    }


    /**
     * android 6.0 以上需要动态申请权限
     */
    private void initPermission(Activity activity) {
        String[] permissions = {
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE
        };

        ArrayList<String> toApplyList = new ArrayList<String>();

        for (String perm : permissions) {
            if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(this, perm)) {
                toApplyList.add(perm);
                // 进入到这里代表没有权限.
            }
        }
        String[] tmpList = new String[toApplyList.size()];
        if (!toApplyList.isEmpty()) {
            ActivityCompat.requestPermissions(activity, toApplyList.toArray(tmpList), 123);
        }
    }

}
